package bscorp.appbase.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * 属性文件M4.properties 获取
 * Created by tanxh on 2017/9/8.
 */

public class M4Properties {
    private static M4Properties m4p;
    private static Properties properties;

    private M4Properties() {
    }

    private void init() {
        properties = new Properties();

        try {
            InputStream is = M4Properties.class.getResourceAsStream("/assets/m4.properties");
            properties.load(is);//is是通过上面获得的输入流
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static M4Properties getInstance() {
        if (m4p == null) {
            synchronized (M4Properties.class) {
                if (m4p == null) {
                    m4p = new M4Properties();
                    m4p.init();
                }
            }
        }
        return m4p;
    }

    /**
     * 获取属性值
     *
     * @param key
     * @return
     */
    public String getConst(String key) {
        return properties.getProperty(key);//通过key获得相应的值
    }

    public static Properties getPropertiesFromFile(String fileName) throws IOException {
        Properties propertiesTemp = new Properties();

        InputStream inputStream = M4Properties.class.getResourceAsStream("/assets/" + fileName);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        propertiesTemp.load(bufferedReader);

        return propertiesTemp;
    }
}
